const gulp = require('gulp');
const less = require('gulp-less');
const rename = require('gulp-rename');
const del = require('del');
const imagemin = require('gulp-imagemin');

/// 定义任务
gulp.task('default', ['deleteDist', 'copyWXML', 'copyJS', 'copyJSON', 'copyWXSS', 'copyWxs', 'copyImages']);

/// 删除dist目录
gulp.task('deleteDist', function () {
  del('dist/**/*.*').then(function (res) {

  });
});

/// 拷贝 wxml
gulp.task('copyWXML', function () {
  gulp.src('src/**/*.wxml').pipe(gulp.dest('dist'));
});

/// 拷贝js
gulp.task('copyJS', function () {
  gulp.src('src/**/*.js').pipe(gulp.dest('dist'));
});

/// 拷贝json
gulp.task('copyJSON', function () {
  gulp.src('src/**/*.json').pipe(gulp.dest('dist'));
});

/// 编译与copy wxss
gulp.task('copyWXSS', function () {
  gulp.src('src/**/*.wxss').pipe(less()).pipe(rename(function (path) {
    path.extname = '.wxss';
  })).pipe(gulp.dest('dist'));
});

/// 拷贝wxs
gulp.task('copyWxs', function () {
  gulp.src('src/**/*.wxs').pipe(gulp.dest('dist'));
});

/// 拷贝图片
gulp.task('copyImages', function () {
  gulp.src('src/**/*.png').pipe(imagemin()).pipe(gulp.dest('dist'));
  gulp.src('src/**/*.jpg').pipe(imagemin()).pipe(gulp.dest('dist'));
});

/// 监听内容改变
gulp.task('watch', function () {
  gulp.watch('src/**/*.*', ['copyWXML', 'copyJS', 'copyJSON', 'copyWXSS', 'copyWxs', 'copyImages']);
});
